#include "SmartHomeMonitorServer.hpp"
#include "UserLoginSection1.hpp"  
#include "UserLoginSection2.hpp"  
#include "UserRegisterSection1.hpp"  
#include "UserRegisterSection2.hpp"
#include "MyLogger.hpp"
#include "TLV.hpp"
#include "Packet.hpp"
#include <iostream>
#include <functional>  // 添加这行，用于 std::bind
#include <memory>      // 添加这行，用于 std::make_shared
using std::cout;
using std::endl;

namespace wd
{
SmartHomeMonitorServer::SmartHomeMonitorServer(unsigned short port, const string & ip, int threadNum, int sz)
    : _threadpool(threadNum, sz) 
      , _tcpServer(port, ip)
    {
        using namespace std::placeholders;
        _tcpServer.setAllCallbacks(
                                   std::bind(&SmartHomeMonitorServer::onConnection, this, _1),
                                   std::bind(&SmartHomeMonitorServer::onMessage, this, _1),
                                   std::bind(&SmartHomeMonitorServer::onClose, this, _1));
    }

void SmartHomeMonitorServer::start()
{
    _threadpool.start();
    _tcpServer.start();
}

void SmartHomeMonitorServer::stop()
{
    _tcpServer.stop();
    _threadpool.stop();
}

void SmartHomeMonitorServer::onConnection(TcpConnectionPtr conn)
{
    LOG_INFO("tcp %s has connected\n",conn->toString().c_str());
}


void SmartHomeMonitorServer::onMessage(TcpConnectionPtr conn)
{
    Packet packet;
    int ret=conn->readPacket(packet);
    cout<<"read:"<<ret<<endl;
    cout<<"packet type:"<<packet._type<<endl;
    cout<<"packet length"<<packet._length<<endl;
    cout<<"packet.msg"<<packet._msg<<endl;
    //选择不同的Type是账号还是密码
    switch(packet._type)
    {
    case TASK_TYPE_LOGIN_SECTION1:
        {
            // 使用智能指针避免拷贝问题
            auto task = std::make_shared<UserLoginSection1>(packet, conn);
            _threadpool.addTask([task]() {
                                task->process();
                                });
        break;
        }
    case TASK_TYPE_LOGIN_SECTION2:
        {
            // 使用智能指针避免拷贝问题
            auto task = std::make_shared<UserLoginSection2>(conn, packet);
            _threadpool.addTask([task]() {
                                task->process();
                                });
        break;
        }
    case TASK_TYPE_REGISTER_SECTION1:
        {
            auto task = std::make_shared<UserRegisterSection1>(conn, packet);
            _threadpool.addTask([task]() {
                                task->process();
                                });
        break;
        }
    case TASK_TYPE_REGISTER_SECTION2:
        {
            auto task = std::make_shared<UserRegisterSection2>(conn, packet);
            _threadpool.addTask([task]() {
                                task->process();
                                });
        break;
        }
    default:
        // 新增：处理未知类型，避免遗漏
        LOG_WARN("Unknown packet type: %d", packet._type);
        break;
    }
}

void SmartHomeMonitorServer::onClose(TcpConnectionPtr conn)
{
    LOG_INFO("tcp %s has closed\n",conn->toString().c_str());
}
}
